import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { BaseParameterModel } from '#/api/basic/base/model/baseParameterModel';

import { ref } from 'vue';

import { useAccess } from '@vben/access';

import { BaseParameterApi } from '#/api/basic/base/baseParameter';
import { PermCode } from '#/enums/perm';
import { $t } from '#/locales';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
  STATE_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.basic.base.baseParameter;

export function createCrudOptions(
  props: CreateCrudOptionsProps<BaseParameterModel.BaseParameterResultVO, any>,
): CreateCrudOptionsRet<BaseParameterModel.BaseParameterResultVO> {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: BaseParameterApi.pageRequest,
        addRequest: BaseParameterApi.addRequest,
        editRequest: BaseParameterApi.editRequest,
        delRequest: BaseParameterApi.delRequest,
        infoRequest: BaseParameterApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: BaseParameterApi.remove,
            role: permCode.delete,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: 200,
        buttons: {
          edit: { show: hasPermission(permCode.edit) },
          view: { show: true },
          remove: { show: hasPermission(permCode.delete) },
          copy: { show: hasPermission(permCode.add) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        name: {
          title: $t('devOperation.system.defParameter.name'),
          type: 'text',
          search: { show: true },
        },
        key: {
          title: $t('devOperation.system.defParameter.key'),
          type: 'text',
          search: { show: true },
        },
        value: {
          title: $t('devOperation.system.defParameter.value'),
          type: 'text',
          search: { show: true },
        },
        state: {
          title: $t('devOperation.system.defParameter.state'),
          type: 'dict-radio',
          dict: STATE_CONSTANT_DICT,
          search: { show: true },
          addForm: { value: true },
        },
        remarks: {
          title: $t('devOperation.system.defParameter.remarks'),
          type: 'textarea',
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = {
  addForm: {
    rules: () => {
      return {};
    },
  },
  editForm: {
    rules: (_: CrudExpose) => {
      return {};
    },
  },
};
